#ifndef __base_h__
#define __base_h__

#include <string>

namespace base
{
    template<typename T>
    std::string i2s( T t )
    {
        std::string s;
        char sz[ sizeof(T) * 8 / 4 + 1 ] = { 0 };
        for( int i = 0; i < sizeof(T) * 8 / 4; i++ )
        {
            unsigned off = sizeof(T) * 8 - 4 * ( i + 1 );
            char p = (char)((t >> off) & 0xF);
            if( p <= 9 && p >= 0 )
            {
                p += '0';
            }
            else if( p >= 0xA && p <= 0xF )
            {
                p += 'a';
                p -= 10;
            }

            sz[ i ] = p;
        }

        s = sz;
        return s;
    }

    template<typename T>
    T hex2i( const char *p, int n )
    {
        T t = 0;
        while( p && *p && n != 0 )
        {
            t <<= 4;
            if( *p >= '0' && *p <= '9' )
            {
                t |= (*p - '0');
            }
            else if( *p >= 'a' && *p <= 'f' )
            {
                t |= (*p - 'a' + 10);
            }
            else if( *p >= 'A' && *p <= 'F' )
            {
                t |= (*p - 'A' + 10);
            }
            else
            {
                return 0;
            }

            p++;
            n--;
        }

        return t;
    }

    std::wstring utf8tounicode( const std::string &text );

    std::string unicodetoutf8( const std::wstring &text );
}

#endif